//Documentation complete
/**
 * @author Jeff
 * @version 2009AP05
 */

package control;

import problemDomain.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import broker.*;

import java.util.List;

/**
 * Control object for menu item ojbect operations.
 */
public class MenuItemControl extends Control {
	
	/* Attributes */
	
	private MenuItemBroker mib;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>MenuItemControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public MenuItemControl () throws ErrorBean {
		mib = MenuItemBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new menu item.
	 * @param menuId the ID of the menu to which the menu item belongs
	 * @param name the name of the menu item
	 * @param price the price of the menu item
	 * @param number the list number of the menu item
	 * @return the new menu item
	 * @throws ErrorBean if the input data are not correct
	 */
	public MenuItemBean create (String menuId, String name, String price, String number) throws ErrorBean {
		int menuIdInt = checkMenuId(menuId);
		MenuBean menu = null;
		if (menuIdInt > 0) {
			menu = new MenuBean(menuIdInt);
			menu.setActive(true);
		}
		
		MenuItemBean menuItem = new MenuItemBean(0, menu, name, null, checkPrice(price), checkMenuItemNumber(number));
		mib.persist(menuItem);
		menuItem.setId(mib.getNewMenuItemID());
		return menuItem;
	}
	
	/**
	 * Searches for menus which match the search criteria.
	 * @param id the ID to search for (0 excludes this field when searching)
	 * @param number the list number to search for (0 excludes this field when searching)
	 * @param name the name to search for (<code>""</code> excludes this field when searching)
	 * @param price the price to search for (<code>""</code> excludes this field when searching)
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<MenuItemBean> search (String id, String number, String name, String price) throws ErrorBean {
		MenuItemBean menuItem = new MenuItemBean();
		
		if (id != null && !"".equals(id))
			menuItem.setId(checkMenuItemId(id));
		
		if (number != null && !"".equals(number))
			menuItem.setNumber(checkMenuItemNumber(number));
		
		if (price != null && !"".equals(price))
			menuItem.setPrice(checkPrice(price));
		
		if (name != null && !"".equals(name))
			menuItem.setName(name);
		
		return mib.search(menuItem);
	}
	
	/**
	 * Updates a menu item.
	 * @param id the ID of the menu item
	 * @param menuId the ID of the menu to which the menu item belongs
	 * @param name the name of the menu item
	 * @param price the price of the menu item
	 * @param number the list number of the menu item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String id, String menuId, String name, String price, String number) throws ErrorBean {
		MenuItemBean menuItem = new MenuItemBean(checkMenuItemId(id), new MenuBean(checkMenuId(menuId)), name, null, checkPrice(price), checkMenuItemNumber(number));
		mib.persist(menuItem);
	}
	
	/**
	 * Deletes a menu item.
	 * @param id the ID of the menu item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		mib.remove(new MenuItemBean(checkMenuItemId(id)));
	}
	
	/**
	 * Lists all menu items.
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<MenuItemBean> listAll () throws ErrorBean {
		return mib.listAll();
	}
	
	/**
	 * Lists all food items in a menu item.
	 * @param id the ID of the menu item
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<FoodItemBean> listFoodItems (String id) throws ErrorBean {
		MenuItemBean searchBean = new MenuItemBean(checkMenuItemId(id));
		List<MenuItemBean> list = mib.search(searchBean);
		if (list.size() > 0)
			searchBean = list.get(0);
		return searchBean.getItemList();
	}
	
	/**
	 * Adds a food item to a menu item.
	 * @param menuItemId the ID of the menu item
	 * @param foodItemId the ID of the food item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void addFoodItem (String menuItemId, String foodItemId) throws ErrorBean {
		mib.addFoodItem(
			new MenuItemBean(checkMenuItemId(menuItemId)),
			new FoodItemBean(checkFoodItemId(foodItemId)));
	}
	
	/**
	 * Removes a food item from a menu item.
	 * @param menuItemId the ID of the menu item
	 * @param foodItemId the ID of the food item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void removeFoodItem(String menuItemId, String foodItemId) throws ErrorBean {
		mib.removeFoodItem(
			new MenuItemBean(checkMenuItemId(menuItemId)),
			new FoodItemBean(checkFoodItemId(foodItemId)));
	}
	
	/* Methods */
	
	
	/* Data Checks */
	
	private int checkMenuItemId (String id) throws ErrorBean {
		return checkInt(id, MENUITEM_ID_INVALID, this);}
	
	private double checkPrice (String price) throws ErrorBean {
		return checkDouble(price, MENUITEM_PRICE_INVALID, this);}
	
	private int checkMenuItemNumber (String number) throws ErrorBean {
		return checkInt(number, MENUITEM_NUMBER_INVALID, this);}
	
	private int checkMenuId (String id) throws ErrorBean {
		return checkInt(id, MENU_ID_INVALID, this);}
	
	private int checkFoodItemId (String id) throws ErrorBean {
		return checkInt(id, FOODITEM_ID_INVALID, this);}
	
	/* END Data Checks */
	
}
